package pers.cz.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import pers.cz.node.Node;
import pers.cz.node.NodeEnum;
import pers.cz.utils.DynamicEnumUtil;
import pers.cz.utils.LogUtils;

/**
 * @program: jef-flow-spring-boot-starter
 * @description: 处理注解，自动注册enum
 * @author: Cheng Zhi
 * @create: 2023-02-08 12:20
 **/
// @Aspect
public class AutoRegisterManager {

    /*@Before(value = "@annotation(pers.cz.annotation.AutoRegister)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        Class<?> classTarget = pjp.getTarget().getClass();
        String className = classTarget.getName();
        // 默认截取去掉后四位的剩余部分
        String nodeName = className.substring(0,className.length()-4);
        LogUtils.debug(nodeName + "注册成功！！！");
        // 进行注册
        registerEnum(nodeName, classTarget);

        return pjp.proceed();
    }*/

    public void init(Class<?> clazz) {
        AutoRegister annotation = clazz.getAnnotation(AutoRegister.class);
        if (annotation != null) {
            String className = clazz.getName();
            // 默认截取去掉后四位的剩余部分
            String nodeName = className.substring(0,className.length()-4);
            LogUtils.debug(nodeName + "注册成功！！！");
            // 进行注册
            registerEnum(nodeName, clazz);
        }
    }
    public void registerEnum(String nodeName, Class nodeClass) {
        // 默认自动添加枚举
        addNodeEnum(nodeName.toUpperCase(), nodeName.toLowerCase(), nodeClass);
    }


    /**
     * 添加枚举
     * @param enumName 枚举名
     * @param code 枚举项1
     * @param nodeClass 枚举项2
     */
    private void addNodeEnum(String enumName, String code, Class<? extends Node> nodeClass) {
        DynamicEnumUtil.addEnum(NodeEnum.class, enumName, new Class<?>[]{String.class, Class.class}, new Object[]{code, nodeClass});
    }
}
